var EventProxy = require('eventproxy');
var tr = require('transliteration');
var services = require('../services');
var replyServ = services.reply;
var _ = require('lodash');

//获取可@的用户列表
exports.mentionedUsers = function (req, res) {
	var ep = new EventProxy();
	ep.fail(function (error) { res.json([]); });

	if (!req.session || !req.session.user)
		ep.throw();
	var users = [];

	ep.all('friends', 'reply_author', function () {
		users = _.uniq(users, function (user) {
			return user._id;
		});
		res.json(users);
	});
	
	//获取朋友
	//todo:
	ep.emit('friends');
	
	//获取评论作者
	var tid = req.params.tid;
	if (!tid)
		ep.emit('reply_author');
	else {
		replyServ.queryList({ topicId: tid }, {}, ep.done(function (msg, replies) {
			if (msg) return ep.emit('reply_author');
			replies.forEach(function (reply) {
				var user = reply.author;
				if (user._id == req.session.user._id)
					return;
				users.push({
					id: user._id,
					name: user.loginName,
					pinyin: tr(user.loginName),
					url: '/user/' + user._id
				});
			});
			ep.emit('reply_author');
		}));
	}
}